package cn.cdeden.system.recycle;

import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.satoken.utils.LoginHelper;
import cn.cdeden.knowledge.api.domain.RemoteFileService;
import cn.cdeden.knowledge.api.domain.RemotePersonalTreeService;
import cn.cdeden.knowledge.api.domain.vo.RemotePersonalTreeVo;
import cn.cdeden.system.api.model.LoginUser;
import cn.cdeden.system.domain.FilesTreeRecycle;
import cn.cdeden.system.domain.enums.FilesSizeType;
import cn.cdeden.system.domain.enums.TreeTypeEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Component("personalRecycle")
public class RecyclePersonal implements RecycleInterface{

    @DubboReference(check = false)
    private final RemotePersonalTreeService remotePersonalTreeService;

    @DubboReference(check = false)
    private final RemoteFileService remoteFileService;



    @Override
    public R foxBaseRecoverFileChooseAPath(List<FilesTreeRecycle> recycleList) {
        LoginUser user = LoginHelper.getLoginUser();
//        FileUserDto user = FileUserUtils.getUser(); //用户信息
        String username = user.getUsername();
        String userId = Long.toString(user.getUserId());
        List<RemotePersonalTreeVo> filesTrees = new ArrayList<>();
        List<RemotePersonalTreeVo> ids = new ArrayList<>();
//        List<PersonalFilesTree> filesTrees = new ArrayList<>();
//        List<PersonalFilesTree> ids = new ArrayList<>();

        recycleList.forEach(tree -> {
            String parentId = tree.getParentId();
            RemotePersonalTreeVo byId = remotePersonalTreeService.getById(parentId);
            if (ObjectUtils.isEmpty(byId)){
                throw new RuntimeException("不可为空");
            }
            String superiorsIds = byId.getSuperiorsIds();
            if (StringUtils.isBlank(superiorsIds)){
                superiorsIds = String.valueOf(parentId);
            }else {
                superiorsIds +=","+ parentId;
            }

            RemotePersonalTreeVo filesTree = MapstructUtils.convert(tree, RemotePersonalTreeVo.class);
            filesTree.setId(tree.getFileTreeId());
            filesTree.setSuperiorsPath(byId.getPath());
            filesTree.setDeleteBy(null);
            filesTree.setDeleteType(null);
            filesTree.setDeleteTime(null);
            filesTree.setDeleteId(null);

            filesTree.setUpdateTime(new Date());
            filesTree.setUpdateId(userId);
            filesTree.setUpdateBy(username);

            filesTree.setSuperiorsIds(superiorsIds);
            Long id = filesTree.getId();

            String newSuperiorsIds = superiorsIds + "," + id;

            //是否重命名处理
            if (remotePersonalTreeService.judgeIsEmpty(filesTree.getCreateId(), filesTree.getCreateBy(), filesTree.getRealName(), parentId,
                filesTree.getType(), filesTree.getSuffix())){
                filesTree.setRealName(filesTree.getRealName() + "_恢复_" + System.currentTimeMillis() );
            }else {
                filesTree.setRealName(filesTree.getRealName() + "_恢复");
            }
            filesTree.setPath(byId.getPath()+"/"+filesTree.getRealName()); //路径

            filesTrees.add(filesTree);
            ids.add(filesTree);

            if (StringUtils.equalsIgnoreCase(tree.getType(), TreeTypeEnum.FOLDER.getType())){
                List<FilesTreeRecycle> list = tree.getSonDataList();
                if (CollectionUtils.isNotEmpty(list)){
                    list.forEach(recycle1 -> {
                        List<RemotePersonalTreeVo> fileTree = getFileTree(recycle1,filesTree.getPath(),userId,username,newSuperiorsIds);
                        filesTrees.addAll(fileTree);
                    });

                }
            }

        });



        if (CollectionUtils.isEmpty(filesTrees)){
            throw new RuntimeException();
        }
        boolean b = remotePersonalTreeService.saveBatch(filesTrees);
        for (RemotePersonalTreeVo tree : ids) {
            //  还原使用空间的大小
            remoteFileService.upDateFolderSizeUpDate(tree.getCreateId(),tree,null,tree.getSize(), FilesSizeType.AddSize);
        }
        return R.ok(b);
//        return BaseRespResult.successResult(b);

    }



    private List<RemotePersonalTreeVo> getFileTree(FilesTreeRecycle recycle , String superiorsPath ,String userId, String username,String superiorsIds){
        List<RemotePersonalTreeVo> filesTrees = new ArrayList<>();
        RemotePersonalTreeVo tree = MapstructUtils.convert(recycle, RemotePersonalTreeVo.class);
        tree.setId(recycle.getFileTreeId());
        tree.setSuperiorsPath(superiorsPath);
        tree.setPath(superiorsPath+"/"+tree.getRealName());

        tree.setDeleteBy(null);
        tree.setDeleteType(null);
        tree.setDeleteTime(null);
        tree.setDeleteId(null);

        tree.setUpdateTime(new Date());
        tree.setUpdateId(userId);
        tree.setUpdateBy(username);
        tree.setSuperiorsIds(superiorsIds);


        String newSuperiorsIds = superiorsIds + "," + recycle.getFileTreeId();
        filesTrees.add(tree);
        if (StringUtils.equalsIgnoreCase(tree.getType(), TreeTypeEnum.FOLDER.getType())){
            List<FilesTreeRecycle> list = recycle.getSonDataList();
            if (CollectionUtils.isNotEmpty(list)){
                list.forEach(recycle1 -> {
                    List<RemotePersonalTreeVo> fileTree = getFileTree(recycle1,tree.getPath(),userId,username,newSuperiorsIds);
                    filesTrees.addAll(fileTree);
                });
//                tree.setSonDataList(filesTrees);
            }
        }
        return filesTrees;
    }




}
